package cn.zwx.config;

import cn.zwx.dispatcher.DispatcherServletProxy;
import cn.zwx.feign.FeignRpcClient;
import cn.zwx.remoting.netty.*;
import cn.zwx.util.BeanFactoryUtils;
import feign.Client;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * @author zhangwenxue
 */
@Configuration
@Import(value ={FeignRpcProperties.class})
@ConditionalOnClass(FeignRpcClient.class)
@ConditionalOnProperty(value = "feign.rpc.enable", matchIfMissing = true)
public class FeignRpcAutoConfiguration {



    @Bean
    public Client feignClient(RemotingClient client) {
        //这里使用我们创建的netty客户端
        return new FeignRpcClient(client);
    }

    @Bean
    public RemotingClient remotingClient (FeignRpcProperties feignRpcProperties) {
        return new NettyRemotingClient(feignRpcProperties);
    }

    @Bean
    public RemotingServer remotingServer(FeignRpcProperties feignRpcProperties,DispatcherServletProxy dispatcherServlet) {
        return new NettyRemotingServer(feignRpcProperties,dispatcherServlet);
    }

    /**
     * 初始化 NettyClientRunner
     * @return NettyClientRunner
     */
    @Bean
    public NettyClientRunner clientController() {
        return new NettyClientRunner();
    }

    /**
     * 初始化 NettyServerRunner
     * @return NettyServerRunner
     */
    @Bean
    public NettyServerRunner serverController() {
        return new NettyServerRunner();
    }


    @Bean
    public BeanFactoryUtils beanFactoryUtils() {
        return new BeanFactoryUtils();
    }

}
